﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using MyDMS.BusinessLayer;
using System.Web.Security;

/// <summary>
/// Summary description for PageBaseClass
/// </summary>
public class PageBaseClass : Page
{
    protected Author _author;

    public PageBaseClass()
    {
        this.Load += new EventHandler(PageBase_Load);
    }

    void PageBase_Load(object sender, EventArgs e)
    {
        if (Session["Author"] != null)
            _author = (Author)Session["Author"];
    }

    protected bool AuthenticateAdmin(string userName, string password)
    {
        OfficeFactory oFactory = new OfficeFactory();
        List<Office> ofc = oFactory.GetAllBy(Office.OfficeFields.Account_Email, userName);
        if (ofc.Count > 0)
        {
            if (ofc[0].Account_Password == password)
            {
                _author = new Author(ofc[0].Office_Code, ofc[0].Account_Email, ofc[0].Office_Code, true);
                return true;
            }
        }
        return false;
    }

    protected bool AuthenticateUser(string userName, string password)
    {
        UserInfoFactory userFactory = new UserInfoFactory();
        List<UserInfo> user = userFactory.GetAllBy(UserInfo.UserInfoFields.User_Name, userName);
        if (user.Count > 0)
        {
            if (user[0].User_Password == password)
            {
                int userOfc_Code = (new DepartmentFactory().GetByPrimaryKey(new DepartmentKeys(user[0].Department_Code))).Office_Code;
                _author = new Author(user[0].User_Code, user[0].User_Name, userOfc_Code, false);
                return true;
            }
        }
        return false;
    }

}
